<?php
/**
 * 
 * @author tangbl
 * 
 */
class OutletAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index() {
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("Outlet");
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "1=1 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$upd = L('cfg_list_itf_btn_modify');
			$del = L('cfg_list_itf_btn_delete');
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'title'    => $row['title'],
						'tags'     => $row['tags'],
						'detail'   => $row['detail'],
						'tel'      => $row['tel'],
						'addtime'  => date('Y-m-d',$row['addtime']),
						'deal'=>'	
					<a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>
					<a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a> '
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
			
		}else{
			//dump(mapList());
			//dump(mapColumn());//创建POI字段列
			$this->display();
		}
	}
	
	/**
	 * 添加
	 */
	public function add(){
		
		if(IS_POST){
			
			$data['city']    = $this->_post("area3");
			$data['ssxadd']  = $this->_post("ssxadd");
			$data['detail']  = $this->_post("detail");
			$data['address'] = $data['ssxadd'].$data['detail'];
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"after"  => "Outlet_after_deal",
				"arr"    => $data
			);
			parent::do_add("Outlet",$param);

		} else {
			
			$lang    = getLang();
			$options = getDictByKey("Dict_address",$lang);
			$this->assign("json",$options["array"]);
		
			$this->assign("jumpUrl",U('Outlet/add'));
			$this->display();
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		
		$id = I('param.id',0,'intval');
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		$model   = M("Outlet");
		$row     = $model->find($id);
		if($row['mapid']){
			$json  = mapStorge(array("id"=>$row['mapid']),"delete");
			if($json['status'] === 0){
				mapAll(true);
			}
			/* else{
				$this->error(L('cfg_form_tipmsg_delfail'));
			} */
		}
		$res  = $model->delete($id);
		if($res) {
			$this->suceess(L('cfg_form_tipmsg_delsucc'));
		} else {
			$this->error(L('cfg_form_tipmsg_delfail'));
		}
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		$id    = $this->_param('id','intval',0);
		if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
		
		if(IS_POST) {
			$data['city']    = $this->_post("area3");
			$data['ssxadd']  = $this->_post("ssxadd");
			$data['detail']  = $this->_post("detail");
			$data['address'] = $data['ssxadd'].$data['detail'];
			$param = array(
				"upload" => array("single"=>array("single")),
				"reply"  => true,
				"after"  => "Outlet_after_deal",
				"arr"    => $data
			);
			parent::do_edit("Outlet",$param);
			
		}else{
			
			$model = D("Outlet");
			$data  = $model->find($id);
			$this->assign("Outlet",$data);
			
			$lang    = getLang();
			$options = getDictByKey("Dict_address",$lang);
			$this->assign("json",$options["array"]);
			
			if($data['area']){
				$dict = D("Dict");
				$row  = $dict->find($data['area']);
				$this->assign("row",$row);
			}
			
			$module    = "Outlet";
			$single    = find_thumb($data['solevar'],'single',$module);
			$this->assign("single",$single);

			$this->display();
		}
	}
	
	/**
	 * 地图门店显示
	 */
	public function lbs(){
		
		$lang    = getLang();
		$options = getDictByKey("Dict_address",$lang);
		$this->assign("json",$options["array"]);
		
		$this->assign("pois",mapAll());
		
		$this->display();
	}
	
}
//仅供测试用
function testCurl(){
	$map['ak']  = C("MAP_AK");
	$map['geotable_id']  = C("MAP_OUTLET_ID");
	$url = "http://api.map.baidu.com/geodata/v2/column/list";
	return curlGet($url,$map);
}
?>